<template>
  <div style="margin-left:120px;">
    <el-upload
      class="avatar-uploader"
      :action="'/aliyun/upload_image'"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      name="image">
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>

    <el-upload
      class="upload-demo"
      ref="uploadVedio"
      action="http://haimo.oss-cn-hangzhou.aliyuncs.com"
      :data="oss"
      :on-remove="handleRemove2"
      :on-success="handlesuccess2"
      :auto-upload="false"
      :show-file-list="false"
      :on-change="beforeUploadVideo">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button
        style="margin-left: 10px;"
        size="small"
        type="success"
        @click="submitUpload2"
        :disabled="isVedioUpload"
        v-loading="isVedioLoading">
        上传到服务器
      </el-button>
      <div slot="tip" class="el-upload__tip">视频大小不超过50MB，视频格式为mp4</div>
      <video controls :src="vedioUrl" v-if="vedioUrl" width="350" height="250"></video>
    </el-upload>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: '',
      oss: {
        key: '',
        OSSAccessKeyId: '',
        policy: '',
        Signature: '',
        host: '',
        success_action_status: 200,
        callback: '',
        expire: 0
      },
      isVedioUpload: false,
      isVedioLoading: false,
      vedioUrl: ''
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handlesuccess2(res) {
      if (res.status) {
        this.$message.error('视频上传失败')
        return true
      }
      this.isVedioLoading = false
      console.log(res)
    },
    beforeUploadVideo(file) {
      this.isVedioUpload = false
      this.vedioUrl = URL.createObjectURL(file.raw)
    },
    async submitUpload2() {
      this.isVedioLoading = true
      const url = `/aliyun/access_token`
      this.axios(url).then(({ status, data }) => {
        const { accessid, callback, dir, expire, host, policy, signature } = data
        this.oss.key = dir
        this.oss.host = host
        this.oss.policy = policy
        this.oss.Signature = signature
        this.oss.callback = callback
        this.oss.OSSAccessKeyId = accessid
        this.oss.expire = expire
        this.$refs.uploadVedio.submit()
      })
    },
    handleRemove2(file) {
      console.log(file)
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin: 50px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
